/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.check.abnormalprocess.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.check.abnormalprocess.entity.AbnormalProcess;
import com.jeeplus.modules.check.abnormalprocess.mapper.AbnormalProcessMapper;
import com.jeeplus.modules.check.abnormalprocess.entity.AbnormalProcessChild;
import com.jeeplus.modules.check.abnormalprocess.mapper.AbnormalProcessChildMapper;

/**
 * 异常主附表Service
 * @author 刘明星
 * @version 2020-11-19
 */
@Service
@Transactional(readOnly = true)
public class AbnormalProcessService extends CrudService<AbnormalProcessMapper, AbnormalProcess> {

	@Autowired
	private AbnormalProcessChildMapper abnormalProcessChildMapper;

	public AbnormalProcess get(String id) {
		AbnormalProcess abnormalProcess = super.get(id);
		abnormalProcess.setAbnormalProcessChildList(abnormalProcessChildMapper.findList(new AbnormalProcessChild(abnormalProcess)));
		return abnormalProcess;
	}

	public List<AbnormalProcess> findList(AbnormalProcess abnormalProcess) {
		return super.findList(abnormalProcess);
	}

	public Page<AbnormalProcess> findPage(Page<AbnormalProcess> page, AbnormalProcess abnormalProcess) {
		return super.findPage(page, abnormalProcess);
	}

	@Transactional(readOnly = false)
	public void save(AbnormalProcess abnormalProcess) {
		super.save(abnormalProcess);
		for (AbnormalProcessChild abnormalProcessChild : abnormalProcess.getAbnormalProcessChildList()){
			if (abnormalProcessChild.getId() == null){
				continue;
			}
			if (AbnormalProcessChild.DEL_FLAG_NORMAL.equals(abnormalProcessChild.getDelFlag())){
				if (StringUtils.isBlank(abnormalProcessChild.getId())){
					abnormalProcessChild.setOut(abnormalProcess);
					abnormalProcessChild.preInsert();
					abnormalProcessChildMapper.insert(abnormalProcessChild);
				}else{
					abnormalProcessChild.preUpdate();
					abnormalProcessChildMapper.update(abnormalProcessChild);
				}
			}else{
				abnormalProcessChildMapper.delete(abnormalProcessChild);
			}
		}
	}

	@Transactional(readOnly = false)
	public void delete(AbnormalProcess abnormalProcess) {
		super.delete(abnormalProcess);
		abnormalProcessChildMapper.delete(new AbnormalProcessChild(abnormalProcess));
	}

}